<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <div slot="header">
          <span>公司设置(角色管理)</span>
        </div>
        <!-- card body -->
        <div>
          <el-tabs>
            <!-- 放置页签1 -->
            <el-tab-pane label="角色管理">
              <!-- 新增角色按钮 -->
              <el-row style="padding: 10px 0">
                <el-button
                  icon="el-icon-plus"
                  size="small"
                  type="primary"
                  @click="addRole"
                >
                  新增角色
                </el-button>
              </el-row>
              <!-- 表格 -->
              <el-table :data="roleList">
                <el-table-column type="index" label="序号" width="120" />
                <el-table-column prop="name" label="角色名称" width="240" />
                <el-table-column prop="description" label="描述" />
                <!-- 默认插槽自定义显示操作按钮 -->
                <el-table-column label="操作">
                  <template #default="{ row }">
                    <el-button size="small" type="success">分配权限</el-button>
                    <el-button
                      size="small"
                      type="primary"
                      @click="editRole(row.id)"
                    >
                      编辑
                    </el-button>
                    <el-button
                      size="small"
                      type="danger"
                      @click="delRole(row.id)"
                    >
                      删除
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
              <!-- 分页组件 -->
              <el-row
                type="flex"
                justify="center"
                align="middle"
                style="height: 60px"
              >
                <!-- 分页组件 -->
                <el-pagination
                  :total="total"
                  :page-size="page.pagesize"
                  layout="prev,pager,next"
                  @current-change="changePage"
                />
              </el-row>
            </el-tab-pane>
            <!-- 放置页签2 -->
            <el-tab-pane label="公司信息">
              <div>黑马</div>
            </el-tab-pane>
          </el-tabs>
        </div>
      </el-card>
    </div>
    <!-- 新增角色弹出层 -->
    <!-- visible:控制弹层的显示 -->
    <el-dialog
      :title="roleForm.id ? '编辑角色' : '添加角色'"
      :visible.sync="showDialog"
      width="50%"
      @close="close"
    >
      <!--  :model="roleForm"=>所有表单数据 -->
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" @click="submitRole">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleList, deleteRole, addRole, getRoleDetail, updateRole } from '@/api/setting.js'
export default {
  data () {
    return {
      // 角色表单数据
      roleForm: {
        name: '',
        description: ''
      },
      // 角色表单的校验数据
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '角色名长度为2~10位', trigger: 'blur' }
        ]
      },
      // 角色列表
      roleList: [],
      total: 0,
      page: {
        page: 1,
        pagesize: 5
      },
      showDialog: false
    }
  },
  created () {
    this.getRole()
  },
  methods: {
    async getRole () {
      const { rows, total } = await getRoleList(this.page)
      // const res = await getRoleList(this.page)

      // console.log('角色数据:', rows)
      // console.log('角色数据:', res)

      this.roleList = rows
      this.total = total
    },
    // 切换页码
    changePage (newPage) {
      this.page.page = newPage
      this.getRole()
    },
    // 删除角色
    delRole (id) {
      // console.log('点击当前角色的ID', id)
      /**
       * 1.让用户确认
       * 2.调用接口
       * 3.刷新列表
       */this.$confirm('确认要删除吗?', '提示').then(async () => {
        await deleteRole(id)
        this.$message.success('删除成功!')
        this.page.page = 1
        this.getRole()
      }).catch(() => {

      })
    },
    // 打开弹层
    addRole () {
      this.showDialog = true
    },
    // 关闭弹层
    close () {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    },
    // 新增或编辑角色
    submitRole () {
      /**
 * 1.整体校验
 * 2.校验通过后调用接口
 * 3.关闭弹层,刷新列表
 */
      // 全局校验
      this.$refs.roleForm.validate(async (isOk) => {
        if (isOk) {
          if (this.roleForm.id) {
            await updateRole(this.roleForm)
            this.$message.success('编辑成功')
          } else {
            await addRole(this.roleForm)
            this.$message.success('添加角色成功')
            this.page.page = 1
          }
          this.getRole()
          this.showDialog = false
        }
      })
    },
    // 编辑角色
    async editRole (id) {
      this.showDialog = true
      const res = await getRoleDetail(id)
      this.roleForm = res
      // console.log('编辑角色:', res)
      // await updateRole(this.roleForm)
    }

  }
}
</script>

<style lang="scss" scoped>
</style>
